import Mock from 'mockjs'
import {baseUrl, useMock} from '@/utils/global'
import * as login from '@/mock/modules/login'
import * as user from '@/mock/modules/user'
import * as loginlog from '@/mock/modules/loginlog'
import * as menu from "@/mock/modules/menu";


fnCreate(login, useMock)
fnCreate(user, useMock)
fnCreate(loginlog, useMock)
fnCreate(menu, useMock)

/**
 * 创建mock模拟数据
 * @param {*} mod 模块
 * @param {*} isOpen 是否开启?
 */
function fnCreate (mod, isOpen = true) {

  if (isOpen) {
    for (let key in mod) {
      ((res) => {
        if (res.isOpen !== false) {
          let url = baseUrl
          if(!url.endsWith("/")) {
            url = url + "/"
          }
          url = url + res.url
          Mock.mock(new RegExp(url), res.type, (opts) => {
            opts['data'] = opts.body ? JSON.parse(opts.body) : null
            delete opts.body
            console.log('\n')
            console.log('%c mock拦截, 请求: ', 'color:red', opts)
            console.log('%c mock拦截, 响应: ', 'color:red', res.data)
            return res.data
          })
        }
      })(mod[key]() || {})
    }
  }
}
